package com.example.litepaldemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.litepal.LitePal;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // create the database and tables
        Button btn_create = (Button)findViewById(R.id.btn_create);
        btn_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LitePal.getDatabase();
                Toast.makeText(MainActivity.this, "Database created.", Toast.LENGTH_SHORT).show();
            }
        });

        // add a record
        Button btn_add = (Button)findViewById(R.id.btn_add);
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0; i<3; ++i){
                    Book book = new Book();
                    book.setName("Book name "+i);
                    book.setAuthor("Author "+i);
                    book.setPages(100);
                    book.setPress("Press "+i);
                    book.save();
                }
                Toast.makeText(MainActivity.this, "3 records added.", Toast.LENGTH_SHORT).show();
            }
        });

        // update a record
        Button btn_update = (Button)findViewById(R.id.btn_update);
        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book = new Book();
                book.setName("Android Books Introduction");
                book.updateAll("id=?","2");
                Toast.makeText(MainActivity.this, "records updated.", Toast.LENGTH_SHORT).show();
            }
        });

        // delete the records
        Button btn_delete = (Button)findViewById(R.id.btn_delete);
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LitePal.deleteAll(Book.class,"id=2");
                Toast.makeText(MainActivity.this, "records deleted.", Toast.LENGTH_SHORT).show();
            }
        });


        // query the records
        Button btn_query = (Button)findViewById(R.id.btn_query);
        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Book> books = LitePal.select("id", "name")
                        .where("id>0")
                        .order("id desc")
                        .find(Book.class);

                int count = 0;
                for(Book book:books){
                    ++count;
                    Log.d("Output",book.getId()+":"+book.getName());
                }



                Toast.makeText(MainActivity.this, count+" records find.", Toast.LENGTH_SHORT).show();
            }
        });





        
    }
}
